package com.luyuan.crm.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class FileReader2 {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                FileReader2.class.getResourceAsStream("province.text"), "UTF-8"));

        String code = null;
        String province = null;
        String city = null;
        String region = null;

        String tempString = null;
        // 一次读入一行，直到读入null为文件结束
        while ((tempString = reader.readLine()) != null) {
            // 显示行号
            code = tempString.substring(0, 6);

            if (!" ".equals(tempString.substring(8, 9))) {
                province = tempString.substring(8);
                city = null;
                region = null;
            }
            else if (!" ".equals(tempString.substring(10, 11))) {
                city = tempString.substring(10);
                region = null;

                if ("市辖区".equals(city)) {
                    city = province;
                }
                else if ("县".equals(city)) {
                    city = province;
                    region = "其它区";

                    continue;
                }
            }
            else {
                region = tempString.substring(12);

                // if ("市辖区".equals(region)) {
                // region = "其它区";
                // }
            }

            if ("省直辖县级行政区划".equals(city) || "自治区直辖县级行政区划".equals(city)) {
                if (region == null) {
                    continue;
                }
                System.out.println("insert into lyt_region (post_code,province,city) value ('" + code + "','"
                        + province + "','" + region + "');");
            }
            else if (city == null) {
                System.out.println("insert into lyt_region (post_code,province) value ('" + code + "','" + province
                        + "');");
            }
            else if (region == null) {
                System.out.println("insert into lyt_region (post_code,province,city) value ('" + code + "','"
                        + province + "','" + city + "');");
            }
            else {
                System.out.println("insert into lyt_region (post_code,province,city,region) value ('" + code + "','"
                        + province + "','" + city + "','" + region + "');");
            }
        }
        reader.close();
    }
}
